package com.fyzk.activity.lottery.jc.beidan;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.app.ActivityTaskManager;
import com.fyzk.activity.lottery.shuzicai.InfoDetailActivity;
import com.fyzk.activity.prize.JZPrizeActivity;
import com.fyzk.activity.user.LoginActivity;
import com.fyzk.activity.user.touzhu.CommonBetActivity;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.newJz.data.QQListView;
import com.fyzk.activity.lottery.shuzicai.klpk.KLPKPopupItemBean;
import com.fyzk.activity.lottery.shuzicai.klpk.KLPKPopupwindow;
import com.fyzk.bean.CurrentPhaseInfo;
import com.fyzk.betcode.AppLotteryCodeListManager;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.utils.HelpUrlConstants;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LogUtil;
import com.fyzk.view.DownMenuPopupwindow;
import com.fyzk.view.FootTitleView;
import com.fyzk.view.ViewCallBack;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

@ContentView(R.layout.ac_beidan)
public class BeiDanActivity extends BaseActivity implements onTopTitleClickListener,
		KLPKPopupwindow.KLPKPopuponItemListener, OnBeiDanCheckBallListener, BeiDan_BF_Pop.OnBeiDanBfPopListener,DownMenuPopupwindow.DownMenuPopupItemListener{

	private int lotteryId = LotteryIDUtils.DCSPF_LOTTERY_ID_INT;
	private String phase = "";
	private int changCi = 0;

	@ViewInject(R.id.beidan_topview)
	private TopTitleView topView;

	@ViewInject(R.id.beidan_listview)
	private QQListView listView_rspf;
	private List<BeiDanBean> infors_rspf = new ArrayList<>();
	private BeiDanAdapter adapter_rspf;

	@ViewInject(R.id.beidan_listview_bf)
	private QQListView listView_bf;
	private List<BeiDanBean> infors_bf = new ArrayList<>();
	private BeiDanAdapter adapter_bf;

	@ViewInject(R.id.beidan_listview_zjq)
	private QQListView listView_zjq;
	private List<BeiDanBean> infors_zjq = new ArrayList<>();
	private BeiDanAdapter adapter_zjq;

	@ViewInject(R.id.beidan_listview_bqc)
	private QQListView listView_bqc;
	private List<BeiDanBean> infors_bqc = new ArrayList<>();
	private BeiDanAdapter adapter_bqc;

	@ViewInject(R.id.beidan_listview_sxds)
	private QQListView listView_sxds;
	private List<BeiDanBean> infors_sxds = new ArrayList<>();
	private BeiDanAdapter adapter_sxds;

	private int[] popup_itemsId = {R.string.beidan_rspf,
			R.string.beidan_bf, R.string.beidan_zjq,
			R.string.beidan_bqc, R.string.beidan_sxds};
	private List<KLPKPopupItemBean> popup_items = new ArrayList<>();
	private KLPKPopupwindow top_popup;
	private int popupId = 0;

	@ViewInject(R.id.beidan_btm_clear)
	private TextView clear_tv;
	@ViewInject(R.id.beidan_btm_sure)
	private TextView sure_tv;
	@ViewInject(R.id.beidan_btm_changci)
	private TextView changCi_tv;
	@ViewInject(R.id.beidan_btm_tishi)
	private TextView yiXuan_tv;

	private DownMenuPopupwindow downMenu;
	private BeiDan_BF_Pop bf_pop;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initView();
		findCurrentPhase();
	}

	@Override
	protected void onResume() {
		super.onResume();
		switch (AppLotteryCodeListManager.betType) {
			case AppLotteryCodeListManager.OKTOBACK:
			case AppLotteryCodeListManager.FIRSTENTER:
				switch (lotteryId) {
					case LotteryIDUtils.DCSPF_LOTTERY_ID_INT:
						adapter_rspf.setInfos(infors_rspf);
						break;
					case LotteryIDUtils.DCBF_LOTTERY_ID_INT:
						adapter_bf.setInfos(infors_bf);
						break;
					case LotteryIDUtils.DCZJQ_LOTTERY_ID_INT:
						adapter_zjq.setInfos(infors_zjq);
						break;
					case LotteryIDUtils.DCBQC_LOTTERY_ID_INT:
						adapter_bqc.setInfos(infors_bqc);
						break;
					case LotteryIDUtils.DCSXDS_LOTTERY_ID_INT:
						adapter_sxds.setInfos(infors_sxds);
						break;
				}
				break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 0) {
			switch (resultCode) {
				case LotteryIDUtils.DCSPF_LOTTERY_ID_INT:
					adapter_rspf.setInfos(infors_rspf);
					break;
				case LotteryIDUtils.DCBF_LOTTERY_ID_INT:
					adapter_bf.setInfos(infors_bf);
					break;
				case LotteryIDUtils.DCZJQ_LOTTERY_ID_INT:
					adapter_zjq.setInfos(infors_zjq);
					break;
				case LotteryIDUtils.DCBQC_LOTTERY_ID_INT:
					adapter_bqc.setInfos(infors_bqc);
					break;
				case LotteryIDUtils.DCSXDS_LOTTERY_ID_INT:
					adapter_sxds.setInfos(infors_sxds);
					break;
			}
		}
	}

	private void initView() {
		topView.setJianTouVisible(View.VISIBLE);
		topView.setOnTitleClickListener(this);
		topView.setRightVisible(View.VISIBLE);
		topView.setJianTouVisible(View.VISIBLE);
		topView.setRight2Visible(View.GONE);
		topView.setOnTitleClickListener(this);
		topView.setRightImg(R.drawable.ic_question);
		for (int i = 0; i < popup_itemsId.length; i++) {
			KLPKPopupItemBean itemBean = new KLPKPopupItemBean();
			itemBean.str1 = getString(popup_itemsId[i]);
			popup_items.add(itemBean);
		}

		top_popup = new KLPKPopupwindow(this,
				AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT,
				popup_items, popup_items.size());
		top_popup.setHm_PopupOnItemListener(this);
		top_popup.setCheckItem(popupId);
		topView.setCenterText(popup_items.get(popupId).str1);

		listView_rspf.setGroupIndicator(null);
		listView_rspf.clearChoices();
		listView_rspf.setDividerHeight(0);
		listView_rspf.setHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.beidan_list_group, null));
		adapter_rspf = new BeiDan_RSPF_Adapter(getContext(), infors_rspf, listView_rspf, this);
		listView_rspf.setAdapter(adapter_rspf);

		listView_bf.setGroupIndicator(null);
		listView_bf.clearChoices();
		listView_bf.setDividerHeight(0);
		listView_bf.setHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.beidan_list_group, null));
		adapter_bf = new BeiDan_BF_Adapter(getContext(), infors_bf, listView_bf, this);
		listView_bf.setAdapter(adapter_bf);

		listView_zjq.setGroupIndicator(null);
		listView_zjq.clearChoices();
		listView_zjq.setDividerHeight(0);
		listView_zjq.setHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.beidan_list_group, null));
		adapter_zjq = new BeiDan_ZJQ_Adapter(getContext(), infors_zjq, listView_zjq, this);
		listView_zjq.setAdapter(adapter_zjq);

		listView_bqc.setGroupIndicator(null);
		listView_bqc.clearChoices();
		listView_bqc.setDividerHeight(0);
		listView_bqc.setHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.beidan_list_group, null));
		adapter_bqc = new BeiDan_BQC_Adapter(getContext(), infors_bqc, listView_bqc, this);
		listView_bqc.setAdapter(adapter_bqc);

		listView_sxds.setGroupIndicator(null);
		listView_sxds.clearChoices();
		listView_sxds.setDividerHeight(0);
		listView_sxds.setHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.beidan_list_group, null));
		adapter_sxds = new BeiDan_SXDS_Adapter(getContext(), infors_sxds, listView_sxds, this);
		listView_sxds.setAdapter(adapter_sxds);

		clear_tv.setOnClickListener(this);
		sure_tv.setOnClickListener(this);
		downMenu = new DownMenuPopupwindow(this,
				AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT,
				getMenuItems());
		downMenu.setDownMenuPopupOnItemListener(this);
	}
	protected String[] getMenuItems() {
		return new String[]{"开奖公告", "玩法介绍", "投注查询"};
	}
	/**
	 * 查询新期
	 */
	private void findCurrentPhase() {
		try {
			JSONObject params = new JSONObject();
			params.put("lotteryType", lotteryId);
			httpPostJson(CommandUtil.FINDCURRENTZCPHASE, Commands.FINDCURRENTZCPHASE, params,
					false);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void requestData() {
		JSONObject params = new JSONObject();
		try {
			params.put("lotteryType", lotteryId);
			params.put("phase", phase);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		httpPostJson(CommandUtil.FINDDCRACE, Commands.FINDDCRACE, params, true);
	}

	@Override
	public void onRightClick() {
		super.onRight2Click();
		toHelp();
	}
	/**
	 * 进入玩法介绍
	 */
	public void toHelp() {
//		if (topView != null) {
//			downMenu.showPopupWindow(topView);
//		} else {
			Intent intent = new Intent(getContext(), InfoDetailActivity.class);
			intent.putExtra(IntentParamUtils.TITLE, getHelpTitle());
			intent.putExtra(IntentParamUtils.URL, getHelpUrl());
			startActivity(intent);
//		}
	}
	@Override
	protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
		super.onSuccess(requestCode, resultBean, result);
		Log.e("fjl","fasfasf"+requestCode);
		switch (requestCode) {
			case CommandUtil.FINDCURRENTZCPHASE:
				List<CurrentPhaseInfo> phaseInfos = JSON.parseArray(resultBean.getData(),
						CurrentPhaseInfo.class);
				if (phaseInfos != null && phaseInfos.size() > 0) {
					phase = phaseInfos.get(0).getPhase();
					AppLotteryInfos.setPeriod(lotteryId,phase);
					requestData();
				} else  {
					AppToast.getToast().show("暂无对阵信息");
				}
				break;
			case CommandUtil.FINDDCRACE:
				try {
					List<BeiDanBean> infors = new ArrayList<>();
					JSONObject obj = new JSONObject(resultBean.getData());
					Iterator<String> iterator = obj.keys();
					ArrayList<String> dateArray = new ArrayList<>();
					while (iterator.hasNext()) {
						dateArray.add(iterator.next());
					}
					Collections.sort(dateArray);
					for (String matchTime : dateArray) {
						BeiDanBean info = new BeiDanBean();
						info.matchTime = matchTime;
						List<BeiDanChildBean> info_teams = JSON.parseArray(obj.getString(matchTime), BeiDanChildBean.class);
						for(int i = 0;i<info_teams.size();i++){
							info_teams.get(i).initBtnHGPojo(lotteryId);
						}
						info.childBeen.addAll(info_teams);
						infors.add(info);
					}

					switch (popupId) {
						case 0:
							infors_rspf.addAll(infors);
							adapter_rspf.setInfos(infors_rspf);
							for(int i = 0; i < adapter_rspf.getGroupCount(); i++){
								listView_rspf.expandGroup(i);
							}
							break;
						case 1:
							infors_bf.addAll(infors);
							adapter_bf.setInfos(infors_bf);
							for(int i = 0; i < adapter_bf.getGroupCount(); i++){
								listView_bf.expandGroup(i);
							}
							break;
						case 2:
							Log.e("fjl1111",JSON.toJSONString(infors));
							infors_zjq.addAll(infors);
							adapter_zjq.setInfos(infors_zjq);
							for(int i = 0; i < adapter_zjq.getGroupCount(); i++){
								listView_zjq.expandGroup(i);
							}
							Log.e("fjl1111",JSON.toJSONString(infors));
							break;
						case 3:
							infors_bqc.addAll(infors);
							adapter_bqc.setInfos(infors_bqc);
							for(int i = 0; i < adapter_bqc.getGroupCount(); i++){
								listView_bqc.expandGroup(i);
							}
							break;
						case 4:
							infors_sxds.addAll(infors);
							adapter_sxds.setInfos(infors_sxds);
							for(int i = 0; i < adapter_sxds.getGroupCount(); i++){
								listView_sxds.expandGroup(i);
							}
							break;
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}
				break;
		}
	}

	@Override
	public void onFailure(int requestCode, String error_code, String message) {
		if (error_code.equals("900003")) {
//			infos_all.clear();
//			infos.clear();
//			adapterChange(infos);
//			mVi_netView.noneRecord(message);
		} else {
//			mVi_netView.fail(message);
		}
	}

	@Override
	public void onCenterClick() {
		super.onCenterClick();
		top_popup.showPopupWindow(topView);
	}

	@Override
	public void onItem(int position) {
		popupId = position;
		top_popup.setCheckItem(popupId);
		topView.setCenterText(popup_items.get(popupId).str1);

		getAdapter(position);
	}

	@Override
	public void popupShow() {
		topView.setJianTouRotate_shun();
	}

	@Override
	public void popupDismiss() {
		topView.setJianTouRotate_ni();
	}

	protected void getAdapter(int popupId) {
		listView_rspf.setVisibility(View.GONE);
		listView_bf.setVisibility(View.GONE);
		listView_zjq.setVisibility(View.GONE);
		listView_bqc.setVisibility(View.GONE);
		listView_sxds.setVisibility(View.GONE);
		switch (popupId) {
			case 0:
				lotteryId = LotteryIDUtils.DCSPF_LOTTERY_ID_INT;
				listView_rspf.setVisibility(View.VISIBLE);
				if (infors_rspf.size() == 0) {
					requestData();
				} else {
					adapter_rspf.notifyDataSetChanged();
				}
				break;
			case 1:
				lotteryId = LotteryIDUtils.DCBF_LOTTERY_ID_INT;
				listView_bf.setVisibility(View.VISIBLE);
				if (infors_bf.size() == 0) {
					requestData();
				} else {
					adapter_bf.notifyDataSetChanged();
				}
				break;
			case 2:
				lotteryId = LotteryIDUtils.DCZJQ_LOTTERY_ID_INT;
				listView_zjq.setVisibility(View.VISIBLE);
				if (infors_zjq.size() == 0) {
					requestData();
				} else {
					adapter_zjq.notifyDataSetChanged();
				}
				break;
			case 3:
				lotteryId = LotteryIDUtils.DCBQC_LOTTERY_ID_INT;
				listView_bqc.setVisibility(View.VISIBLE);
				if (infors_bqc.size() == 0) {
					requestData();
				} else {
					adapter_bqc.notifyDataSetChanged();
				}
				break;
			case 4:
				lotteryId = LotteryIDUtils.DCSXDS_LOTTERY_ID_INT;
				listView_sxds.setVisibility(View.VISIBLE);
				if (infors_sxds.size() == 0) {
					requestData();
				} else {
					adapter_sxds.notifyDataSetChanged();
				}
				break;
		}

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
			case R.id.beidan_btm_clear:
				switch (popupId) {
					case 0:
						for (int i = 0; i < infors_rspf.size(); i++) {
							List<BeiDanChildBean> childBeens = infors_rspf.get(i).childBeen;
							for (int j = 0; j < childBeens.size(); j++) {
								BeiDanChildBean childBean = childBeens.get(j);
								childBean.clearRspf();
							}
						}
						adapter_rspf.setInfos(infors_rspf);
						break;
					case 1:
						for (int i = 0; i < infors_bf.size(); i++) {
							List<BeiDanChildBean> childBeens = infors_bf.get(i).childBeen;
							for (int j = 0; j < childBeens.size(); j++) {
								BeiDanChildBean childBean = childBeens.get(j);
								childBean.clearBf();
							}
						}
						adapter_bf.setInfos(infors_bf);
						break;
					case 2:
						for (int i = 0; i < infors_zjq.size(); i++) {
							List<BeiDanChildBean> childBeens = infors_zjq.get(i).childBeen;
							for (int j = 0; j < childBeens.size(); j++) {
								BeiDanChildBean childBean = childBeens.get(j);
								childBean.clearZjq();
							}
						}
						adapter_zjq.setInfos(infors_zjq);
						break;
					case 3:
						for (int i = 0; i < infors_bqc.size(); i++) {
							List<BeiDanChildBean> childBeens = infors_bqc.get(i).childBeen;
							for (int j = 0; j < childBeens.size(); j++) {
								BeiDanChildBean childBean = childBeens.get(j);
								childBean.clearBqc();
							}
						}
						adapter_bqc.setInfos(infors_bqc);
						break;
					case 4:
						for (int i = 0; i < infors_sxds.size(); i++) {
							List<BeiDanChildBean> childBeens = infors_sxds.get(i).childBeen;
							for (int j = 0; j < childBeens.size(); j++) {
								BeiDanChildBean childBean = childBeens.get(j);
								childBean.clearSxds();
							}
						}
						adapter_sxds.setInfos(infors_sxds);
						break;
				}
				break;
			case R.id.beidan_btm_sure:
				if (changCi == 0) {
					AppToast.getToast().show("请至少选择一场比赛");
				} else {
					toPay();
				}
				break;
		}
	}

	@Override
	public void onCheckBall(int changCi) {
		this.changCi = changCi;
		changCi_tv.setText(changCi+"场");
	}

	@Override
	public void onZhanKaiClick(BeiDanChildBean infor) {
		if (bf_pop == null) {
			bf_pop = new BeiDan_BF_Pop(getContext(),infor, this);
		}
		bf_pop.showPopupWindow(sure_tv, infor);
	}

	@Override
	public void onSureClick() {
		adapter_bf.notifyDataSetChanged();
	}

	protected Class getPayClass() {
		switch (lotteryId) {
			case LotteryIDUtils.DCSPF_LOTTERY_ID_INT:
				return BeiDan_RSPF_PayActivity.class;
			case LotteryIDUtils.DCBF_LOTTERY_ID_INT:
				return BeiDan_BF_PayActivity.class;
			case LotteryIDUtils.DCZJQ_LOTTERY_ID_INT:
				return BeiDan_ZJQ_PayActivity.class;
			case LotteryIDUtils.DCBQC_LOTTERY_ID_INT:
				return BeiDan_BQC_PayActivity.class;
			case LotteryIDUtils.DCSXDS_LOTTERY_ID_INT:
				return BeiDan_SXDS_PayActivity.class;
		}
		return BeiDan_RSPF_PayActivity.class;
	}

	protected ArrayList<BeiDanChildBean> getPayInfors() {
		Log.e("fjl-----",JSON.toJSONString(infors_zjq));
		ArrayList<BeiDanChildBean> infors = new ArrayList<>();
		List<BeiDanBean> infors_old = new ArrayList<>();
		switch (lotteryId) {
			case LotteryIDUtils.DCSPF_LOTTERY_ID_INT:
				infors_old = infors_rspf;
				break;
			case LotteryIDUtils.DCBF_LOTTERY_ID_INT:
				infors_old = infors_bf;
				break;
			case LotteryIDUtils.DCZJQ_LOTTERY_ID_INT:
				infors_old = infors_zjq;
				break;
			case LotteryIDUtils.DCBQC_LOTTERY_ID_INT:
				infors_old = infors_bqc;
				break;
			case LotteryIDUtils.DCSXDS_LOTTERY_ID_INT:
				infors_old = infors_sxds;
				break;
		}
		for (int i = 0; i < infors_old.size(); i++) {
			for (int j = 0; j < infors_old.get(i).childBeen.size(); j++) {
				BeiDanChildBean childBean = infors_old.get(i).childBeen.get(j);
				if (childBean.checkNum > 0) {
					infors.add(childBean);
				}
			}
		}
		Log.e("fjl",JSON.toJSONString(infors));
		return infors;
	}

	private void toPay() {
		Intent intent = new Intent();
		intent.setClass(getContext(), getPayClass());
		BeiDanBasePayActivity.pay_infors = getPayInfors();
		startActivityForResult(intent, 0);
	}


	private String getHelpTitle() {
		return "北京单场玩法介绍";
	}

	private String getHelpUrl() {
		return HelpUrlConstants.Help_BJDC;
	}

	@Override
	public void onMenuItem(int position) {
		LogUtil.d("" + getMenuItems()[position]);
		Intent intent = null;
		switch (position) {
			case 0:
				// 开奖公告
				intent = new Intent();
				intent.setClass(getContext(), JZPrizeActivity.class);
				intent.putExtra(IntentParamUtils.LOTTERYID, LotteryIDUtils.JZBF_LOTTERY_ID_INT);
				break;
			case 1:
				// 玩法介绍
				intent = new Intent(getContext(), InfoDetailActivity.class);
				intent.putExtra(IntentParamUtils.TITLE, getHelpTitle());
				intent.putExtra(IntentParamUtils.URL, getHelpUrl());
				break;
			case 2:
				// 投注查询
				if (MySharedPreferences.getInstance().getLoginStatus()) {
					intent = new Intent(getContext(), CommonBetActivity.class);
					intent.putExtra(IntentParamUtils.LOTTERYID, LotteryIDUtils
							.JZRFSPF_LOTTERY_ID_INT);
				} else {
					AppLotteryInfos.clear();
					ViewCallBack.getInstance().countDownObjs.clear();
					ActivityTaskManager.getInstance().closeAllActivity();
					intent = new Intent(getContext(), LoginActivity.class);
				}
				break;
			default:
				break;
		}
		try {
			startActivity(intent);
		} catch (Exception e) {
			LogUtil.v(e.getMessage());
		}
	}

	@Override
	public void menuShow() {

	}

	@Override
	public void menuDismiss() {

	}
}
